package com.googlecode.perfectable.visuable.event.handler;

import com.googlecode.perfectable.prosperable.message.ClientMessage;
import com.googlecode.perfectable.visuable.event.ButtonPushEvent;
import com.googlecode.perfectable.visuable.event.CompositeEventListener;
import com.googlecode.perfectable.visuable.event.EventListener;
import com.googlecode.perfectable.visuable.message.client.ButtonPushMessage;
import com.googlecode.perfectable.visuable.visual.custom.ButtonVisual;

public class ButtonPushEventHandler implements EventHandler<ButtonPushEvent> {
	private final CompositeEventListener<ButtonPushEvent> listeners = CompositeEventListener.create();
	private final ButtonVisual target;

	private ButtonPushEventHandler(ButtonVisual target) {
		this.target = target;
	}

	public static ButtonPushEventHandler create(ButtonVisual target) {
		return new ButtonPushEventHandler(target);
	}

	@Override
	public void receive(ClientMessage message) {
		if(!(message instanceof ButtonPushMessage)) {
			return;
		}
		ButtonPushMessage buttonPushMessage = (ButtonPushMessage) message;
		if(buttonPushMessage.isDirectedAt(this.target)) {
			ButtonPushEvent event = new ButtonPushEvent() {
				// MARK source?
			};
			this.listeners.receive(event);
		}
	}

	public void listen(EventListener<ButtonPushEvent> listener) {
		this.listeners.add(listener);
	}
}
